package com.demo.controller;

import com.common.Dto.RegisterDTO;
import com.common.Dto.loginDto;
import com.common.Dto.rechargeDTO;
import com.common.VO.*;
import com.common.commont.Messageconstant;
import com.common.result.Result;
import com.demo.interceptor.JwtProperties;
import com.demo.server.impl.userServerImpl;
import com.tools.untils.jwtutils;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.config.exceptions.ConfigException;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping(("/user"))
public class user {
    @Autowired
    userServerImpl user;
    @Autowired
    JwtProperties jwtProperties;
    /**
     * 用户登录
     * @param loginDto 传递形参
     * @return  是否通过
     */
    @GetMapping(value = "/login", produces = "application/json")
    public Result<LoginVO> login(@RequestBody loginDto loginDto) {
        if(loginDto.getPassword() == null || loginDto.getPassword().equals("")){
            return Result.error(Messageconstant.ERROR_ACCOUNT_PASSWORD);
        }
        log.info("用户登录");
        LoginVO login = user.login(loginDto);
        if (login != null) {
            Map<String,Object> map = new HashMap<>();
            map.put(Messageconstant.user_ID,login.getToken());
            String token = jwtutils.createJWT(
                    jwtProperties.getUserSecretKey(),
                    jwtProperties.getUserTtl(),
                    map
            );
            login.setToken(token);
            return Result.sucess(login);
        }
        return Result.error(Messageconstant.PASSWORD_ERROR);
    }

    /**
     * 用户注册
     * @param registerDTO  传递形参
     * @return  是否通过
     * @throws ConfigException
     */
    @PostMapping("/register")
    public Result<RegisterVO>  register(@RequestBody RegisterDTO registerDTO) throws ConfigException {
        log.info("用户注册");
        return user.register(registerDTO);
    }

    @GetMapping("/getinformaion")
    public Result<InformationVO> GetInformation(){
        InformationVO informationvo = user.getInformation();
        return Result.sucess(informationvo);
    }

    /**
     *创建支付账户
     * @param paypassword
     * @return
     */
    @GetMapping("/createpayment")
    public Result<createpaymentVO> get(String paypassword){
        return user.createpayment(paypassword);
    }

    /**
     * 用户充值
     * @param recharge
     * @return
     * @throws ContractException
     */
    @PostMapping("/recharge")
    public Result<rechargeVO> recharge(rechargeDTO recharge) throws ContractException {
        return  user.recharge(recharge);
    }
}
